package model
{
	
	import vo.CharacterVO;
	import vo.UserVO;
	import remoting.Server;
	
	import manager.*;
	import remoting.*;
	import log.Logger;
	
	/**
	 * -- UserPx --
	 * @desc	proxy for managing user's data
	 * @author	Aqua (built on 2011-11-09)
	 */
	public class UserPx extends BasePx
	{
		//-----------------//
		//     Publics     //
		//-----------------//
		
		/**************************************************************************
		 * -- init --
		 * @desc		initialize proxy
		 *************************************************************************/
		public override function init():void
		{
			_inited		= false;
			_isNewComer	= true;
			_userVO		= new UserVO();
			_settingXML = ResourceMgr.instance.getRes("xml_settings");
			_charaPx	= ProxyMgr.instance.getProxy(CharacterPx.NAME) as CharacterPx;
		}
		
		/**************************************************************************
		 * -- initUser --
		 * @desc		check if user is new comer, create user's profile if yes, or
		 * 				load user's profile from server if not
		 *************************************************************************/
		public function initUser():void
		{
			if (_inited)
			{
				return;
			}
			
			if (-1 != GameMgr.instance.localUser)
			{
				getUserData(GameMgr.instance.localUser, _userVO, response);
				return;
			}
			
			Server.call(Method.GET_CURRENT_USER, onResult, {});
			
			function onResult(res:Object):void
			{
				_isNewComer = ! res["inited"];
				Logger.log("New comer: " + String(_isNewComer));
				
				if (_isNewComer)
				{
					buildUserData(res["uid"], res["username"], response);
				}
				else
				{
					getUserData(res["uid"], _userVO, response);
				}
			}
			
			function response():void
			{
				_charaPx.initCharaList(_isNewComer);
				_inited = true;
			}
		}
		
		/**************************************************************************
		 * -- (getter) inited --
		 * @desc		get that if UserPx is inited
		 * @return		if is inited
		 *************************************************************************/
		public function get inited():Boolean
		{
			return _inited;
		}
		
		/**************************************************************************
		 * -- (getter) isNewComer --
		 * @desc		get that if user is a new comer
		 * @return		true for new comer false for not
		 *************************************************************************/
		public function get isNewComer():Boolean
		{
			return _isNewComer;
		}
		
		/**************************************************************************
		 * -- (getter) userVO --
		 * @desc		get current user's data
		 * @return		user's data
		 *************************************************************************/
		public function get userVO():UserVO
		{
			return _userVO;
		}
		
		/**************************************************************************
		 * -- getUserName --
		 * @desc		get user's name
		 * @return		name
		 *************************************************************************/
		public function getUserName():String
		{
			return _userVO.name;
		}
		
		/**************************************************************************
		 * -- getPower --
		 * @desc		get user's battle power
		 * @return		power
		 *************************************************************************/
		public function getPower():int
		{
			// to do
			return 99999;
		}
		
		/**************************************************************************
		 * -- getUserData --
		 * @desc		get data of user from server
		 * @param		(uid) id of the user
		 * @param		(userVO) container of user's data
		 * @param		(response) response function on data got
		 *************************************************************************/
		public function getUserData(uid:int, userVO:UserVO, response:Function):void
		{
			Server.call(Method.GET_USER_DATA, onResult, { "uid":uid } );
			
			function onResult(res:Object):void
			{
				userVO.uid				= res.uid;
				userVO.name				= res.name;
				userVO.gold				= res.gold;
				userVO.hp				= res.hp;
				userVO.maxHp			= res.maxHp;
				userVO.numDuel			= res.numDuel;
				userVO.numWin			= res.numWin;
				userVO.numDuelLeft		= res.numDuelLeft;
				userVO.winSeries		= res.winSeries;
				userVO.loseSeries		= res.loseSeries;
				userVO.currentSeries	= res.currentSeries;
				
				_isNewComer = !res.inited;
				
				response();
			}
		}
		
		/**************************************************************************
		 * -- deleteUserData --
		 * @desc		delete a user's data
		 * @param		(uid) id of the user
		 * @param		(response) response function on data deleted
		 *************************************************************************/
		public function deleteUserData(uid:int, response:Function):void
		{
			Server.call(Method.DELETE_USER_DATA, onResult, { "uid":uid } );
			
			function onResult(res:Object):void
			{
				response();
			}
		}
		
		/**************************************************************************
		 * -- updateUserData --
		 * @desc		update user's data
		 * @param		(response) response function on data deleted
		 *************************************************************************/
		public function updateUserData(response:Function):void
		{
			_userVO.gold = _userVO.gold + 1;
			
			Server.call(Method.UPDATE_USER_DATA, onResult, _userVO);
			
			function onResult(res:Object):void
			{
				response();
			}
		}
		
		//------------------//
		//     Privates     //
		//------------------//
		
		/**************************************************************************
		 * -- buildUserData --
		 * @desc		initialize profile for new user
		 * @param		(uid) user's id
		 * @param		(username) user's name
		 * @param		(response) response function when operation completed
		 *************************************************************************/
		private function buildUserData(uid:int, username:String, response:Function):void
		{
			var userInitialData:XML;
			
			userInitialData			= _settingXML.user_initial_data[0];
			_userVO.uid				= uid;
			_userVO.name			= username;
			_userVO.gold			= userInitialData.gold;
			_userVO.hp				= userInitialData.hp;
			_userVO.maxHp			= userInitialData.max_hp;
			_userVO.numDuel			= userInitialData.num_duel;
			_userVO.numWin			= userInitialData.num_win;
			_userVO.numDuelLeft		= userInitialData.num_duel_left;
			_userVO.winSeries		= userInitialData.win_series;
			_userVO.loseSeries		= userInitialData.lose_series;
			_userVO.currentSeries	= userInitialData.currentSeries;
			
			Server.call(Method.BUILD_USER_DATA, resp, _userVO);
			
			function resp(returnValue:Object):void
			{
				response();
			}
		}
		
		
		public static const NAME:String = "UserPx";
		
		private var _charaPx	:CharacterPx;
		
		private var _userVO		:UserVO;
		private var _settingXML	:XML;
		private var _inited		:Boolean;
		private var _isNewComer	:Boolean;
	}
	
}